package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.utils.UserThreadLocalUtils;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {

    @Autowired
    private CustomerReservationService customerReservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        String updateBy = UserThreadLocalUtils.getUserId();

        return success( customerReservationService.getCount(Long.valueOf(updateBy)));
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult getTime(@RequestParam(required = false) Long time){
        List<TimeVo> list = customerReservationService.getTime(LocalDateTimeUtil.of(time));
        return success(list);
    }


    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult add(@RequestBody ReservationDto dto){
        customerReservationService.add(dto);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<ReservationVo>> listAll(@ApiParam(value = "当前页码", defaultValue = "1") Integer pageNum,
                                                               @ApiParam(value = "当前页码", defaultValue = "10") Integer pageSize,
                                                               @ApiParam(value = "状态", required = false) Integer status){
        PageResponse<ReservationVo> response = customerReservationService.listAll(pageNum,pageSize,status);
        return success(response);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult removal(@PathVariable Long id){
        customerReservationService.removal(id);
        return success();
    }
}